home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 4 / BBS in a Box - Macintosh - Volume IV (January 1992) (BBS in a Box).iso / Files / Prog / N-P / PROC PARAMS < prev    next >
Encoding:
Text File  |  1987-07-08  |  3.4 KB  |  107 lines  |  [TEXT/MACA]

  1. {
  2. I found the following in Borland's programming SIG. It shows how to
  3. implement procedural parameters in Turbo and TML.  I have mixed feelings
  4. about procedural parameters.  They are included in the ISO standard and
  5. in MPW Pascal.  On the other hand, they're tricksy boogers and I suspect
  6. that most programs would be better if they were written without them.
  7. Like the GoTo, they are anacronisms. Nuff said.  If you need em, here
  8. they are.
  9. }
  10.  
  11. PROGRAM Example_of_Procedure_Parameters(input,output);
  12.  
  13. {              Shows how to use the pass procedures as parameters
  14.        
  15.  Author: Mike Babulic (Compuserve ID 72307,314)
  16.              3827 Charleswood Dr. N.W.,
  17.                 Calgary, Alberta
  18.              CANADA, T2L 2C7
  19.  
  20.  One of the weaknesses of TURBO Pascal is that procedures and functions
  21.  cannot be passed as parameters. Here is a method to overcome this limitation.
  22.  
  23.  Procedures CAN be passed as parameters to other procedures by creative use
  24.  of the INLINE statement!
  25.  
  26.  It is accomplished by defining a dummy procedure (or function) with exactly
  27.  the same arguments as the procedure to be passed EXCEPT that a final added
  28.  parameter of type ProcPtr is also defined. When the calling procedure calls
  29.  the dummy procedure, it passes the address of the procedure to be executed in
  30.  this last parameter.
  31.  
  32.  NOTE: The procedure parameter MUST BE A GLOBAL PROCEDURE or strange & wonderful
  33.        things may happen to your program!!!!
  34. }
  35.  
  36.                 {TML Pascal
  37. USES  MacIntf;
  38. }
  39.                 {TURBO Pascal}
  40.  
  41. USES MemTypes;
  42.    
  43.  
  44. {----------------- Procedures to be passed as parameters -----------------}
  45. {n.b.Procedures MUST be GLOBAL}
  46.  
  47.  PROCEDURE theFirst(a,b:integer);
  48.     begin
  49.       WriteLn(' FIRST ',a:3,b:3);
  50.     end;
  51.  
  52.   PROCEDURE theSecond(a,b:integer);
  53.     begin
  54.       WriteLn(a:3,' SECOND',b:3);
  55.     end;
  56.  
  57.   PROCEDURE theThird(a,b:integer);
  58.     begin
  59.       WriteLn(a:3,b:3,' THIRD');
  60.     end;  
  61.  
  62. {------------------ Routine with procedure parameter ------------------}
  63.     
  64.   PROCEDURE OneTwo(p:ProcPtr);  { <-- procedure p is passed as a parameter}
  65.                                 {     n.b. p MUST be a GLOBAL procedure}
  66.      PROCEDURE dummy(a,b:integer;p:ProcPtr); { <-- dummy procedure call}
  67.        INLINE 
  68.            $205F,      {MOVE.L (SP)+,A0    }
  69.            $4E90;      {JSR    (A0)        }
  70.     begin
  71.       dummy(9,10,p);
  72.     end;
  73.  
  74. {---------------- Procedure Pointers Stored in an Array --------------}
  75.     
  76.   VAR ProcArray : ARRAY [1..3] OF ProcPtr;{ <-- array of Procedure Pointers}
  77.                                           {     n.b. MUST be GLOBAL procedures}
  78.   PROCEDURE dummy(a,b:integer;p:ProcPtr);{ <-- p is stored in the array!}  
  79.      INLINE
  80.            $205F,      {MOVE.L (SP)+,A0    }
  81.            $4E90;      {JSR    (A0)        }
  82.  
  83. {------------------ Demonstrate Procedure Pointers  -----------------}
  84.  
  85.   VAR
  86.       i : integer;  
  87.       c:char;
  88.  
  89.   BEGIN
  90.   
  91.     {Using Procedure Parameters}
  92.        OneTwo(@theFirst);    {will Execute theFirst}
  93.        OneTwo(@theSecond);   {will Execute theSecond}
  94.        OneTwo(@theThird);    {will Execute theThird}
  95.        
  96.     WriteLn;
  97.  
  98.     {Using an array of Procedure Pointers}
  99.        ProcArray[1] := @theFirst;    
  100.        ProcArray[2] := @theSecond;
  101.        ProcArray[3] := @theThird;
  102.        for i := 3 downto 1 do
  103.          dummy(i,i+1,ProcArray[i]);
  104.       
  105.     WriteLn; WriteLn('Press RETURN to Quit');
  106.     Read(c);
  107.   END.